package cn.sl;


import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.io.StringReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: XmlDemo
 * @Author: sl
 * @Date: 2023/7/15 21:14
 * @Since: JDK1.8
 * @Version: 1.0
 */
public class XmlDemo {
    public static void main(String[] args) throws DocumentException, IllegalAccessException {
        /**
         * 新建一个xml对象，将数据写入其中并转为字符串返回结果
         */
        String xmlStr = write();
        /**
         * 读取xml中某个元素的值
         */
        read(xmlStr);
        /**
         * sax 直接查找某个元素的值
         */
        read2(xmlStr);
        /**
         * jaxen
         */
        read3(xmlStr);


    }

    private static void read3(String xmlStr) throws DocumentException {
        long start = System.currentTimeMillis();
        Document document = DocumentHelper.parseText(xmlStr);
        System.out.println(document.selectNodes("//name").get(0).getText());
        System.out.println("jaxen:::"+(System.currentTimeMillis() - start));

//        XPath xpath = Jaxen.getXPathParser().compile("//name");
//        List<Element> nodes = (List<Element>) xpath.evaluate(document);
//        for (Element element : nodes) {
//            String nameValue = element.getTextTrim();
//            System.out.println("Name: " + nameValue);
//        }
    }

    private static void read2(String xmlStr) throws DocumentException {
        long start = System.currentTimeMillis();
        SAXReader reader = new SAXReader();
        Document document = reader.read(new StringReader(xmlStr));
        XPath xpath = DocumentHelper.createXPath("//name");
        String name = xpath.selectNodes(document).get(0).getText();
        System.out.println("sax:::"+(System.currentTimeMillis() - start));
//        List<Node> nodes = xpath.selectNodes(document);
//        for (Node node : nodes) {
//            String nameValue = node.getText();
//            System.out.println("Name: " + nameValue);
//        }
    }

    private static String write() throws IllegalAccessException {
        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("UTF-8");
        //根节点名称
        Element root = document.addElement("root");
        Element appid = root.addElement("aid");
        appid.setText("1010");
        Element pwd = root.addElement("pwd");
        pwd.setText("123456");
        Element irid = root.addElement("rid");
        irid.setText("10001");
        Element type = root.addElement("type");
        type.setText("1");
        Element rs = root.addElement("rows");
        List<User> userList = new ArrayList<>();
        userList.add(new User("1", "张三1", "13"));
        userList.add(new User("2", "张三2", "14"));
        userList.add(new User("3", "张三3", "15"));
        for (User user : userList) {
            //遍历写入多个元素
            Element r = rs.addElement("row");
            Field[] fields = user.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                if ("serialVersionUID".equals(name)) {
                    continue;
                }
                Element element = r.addElement(name);
                element.setText(field.get(user).toString());
            }
        }

        String xmlStr = document.asXML();
        System.out.println(xmlStr);
        return xmlStr;
    }

    private static void read(String xmlStr) throws DocumentException {
        long start = System.currentTimeMillis();
        //字符串转为documen对象
        Document document = DocumentHelper.parseText(xmlStr);
        Element rootElement = document.getRootElement();
        System.out.println("111111111111");
        //获取根节点下的某个元素
        Element rowsResult = rootElement.element("rows");
        Element rowResult = rowsResult.elements("row").get(1);
        Element name = rowResult.element("name");
        String nameStr = name.getData().toString();
        System.out.println(nameStr);
        System.out.println("普通：：："+(System.currentTimeMillis()-start));
    }


}
